<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')) {
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

// Admin group add and amend ajax
function submit_form($type, $value, $submit)
{
    global $LANGUAGE,$LANGLIST,$SETTING,$CONFIGURE,$MEMBER,$ADMIN,$db,$sys,$c,$setting_cover,$setting_attach,$sys,$ac;
    $ajax_response = new xajaxResponse();
    $error = false;
    //$ajax_response->alert(print_r($value, true)); $ajax_response->assign($submit,"disabled",false); return $ajax_response;

    $submit_value   = "编辑";

    if(!empty($value['config'])){
        foreach($value['config'] as $name => $option ){
            $config_info = func::db_select(DB_TABLE_CONFIG, "subject, if_multi, param", "name='".$name."'");
            $config_info[0]['param'] = unserialize($config_info[0]['param']);
            if ($config_info[0]['if_multi'] != "1") {
                if (!preg_match($config_info[0]['param'][0]['check'], $option)){
                    $error = true;
                    if(empty($anchor)) { $anchor = $name; }
                    $ajax_response->assign($name."_0_return", "innerHTML", "<span class=\"prompt_failed\">不符合要求,请返回重新填写!<!-- ".$name." error --></span>");
                } else {
                    $ajax_response->assign($name."_0_return", "innerHTML", "");
                }
            } else {
                foreach($config_info[0]['param'] as $k => $v ){
                    if (!preg_match($v['check'], $option[$v['name']])){
                        $error = true;
                        if(empty($anchor)) { $anchor = $name; }
                        $ajax_response->assign($name."_".$k."_return", "innerHTML", "<span class=\"prompt_failed\">不符合要求,请返回重新填写!<!-- ".$name." error --></span>");
                    } else {
                        $ajax_response->assign($name."_".$k."_return", "innerHTML", "");
                    }
                }
            }
        }
    }
    if($error){
        $ajax_response->assign($submit,"value",$submit_value);
        $ajax_response->assign($submit,"disabled",false);
        $ajax_response->redirect("#".$anchor);
    }else{
        if(!empty($value['config'])){
            foreach($value['config'] as $name => $option ){
                $config_info = func::db_select(DB_TABLE_CONFIG, "subject, if_multi, param", "name='".$name."'");
                $config_info[0]['param'] = unserialize($config_info[0]['param']);
                if ($config_info[0]['if_multi'] != "1") {
                    $sql = "UPDATE ".DB_TABLE_CONFIG."
                            SET value='".addslashes(trim($option))."'
                            WHERE name='".$name."'";
                    $result = &$db->Execute($sql);
                    if (!$result) {
                        $ajax_response->alert($db->ErrorMsg());
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    }
                } else {
                    $sql = "UPDATE ".DB_TABLE_CONFIG."
                            SET value='".addslashes(serialize($option))."'
                            WHERE name='".$name."'";
                    $result = &$db->Execute($sql);
                    if (!$result) {
                        $ajax_response->alert($db->ErrorMsg());
                        $ajax_response->assign($submit,"value",$submit_value);
                        $ajax_response->assign($submit,"disabled",false);
                    }
                }
            }
        }
        $sys->set_prompt(array(
            "prompt_type"       => "'prompt'",
            "prompt_if"         => "1",
            "prompt_message"    => "'配置文件修改完毕'",
            "prompt_returnurl"  => "'".$CONFIGURE['common']['control_admin']."?act=index'",
            "prompt_returntext" => "'返回首页'",
            "prompt_image"      => "'succeed.gif'",
            "prompt_act"        => "'prompt'",
        ));
        $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=prompt");
    }
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "submit_form");
//////

$config_sort_sub    = array();
$config_sort_info   = array();
$config_list        = array();

if (preg_match("/^[0-9]+$/",$sys->get['cs_id'])) {
    $config_sort_info  = func::db_select(DB_TABLE_CONFIG_SORT, "*", "cs_id=".$sys->get['cs_id']);
    $config_sort_info  = $config_sort_info[0];
    if (!empty($config_sort_info)) {
        $sql = "SELECT *
                FROM ".DB_TABLE_CONFIG."
                WHERE cs_id=".$sys->get['cs_id']."
                ORDER BY rank";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }else {
            while(!$result->EOF){
                if(!empty($result->fields['param'])){
                    $param = unserialize($result->fields['param']);
                }
                if ($result->fields['if_multi'] == "1"){
                    $value_array = unserialize($result->fields['value']);
                }
                $config_list[] = array(
                                "name"          => $result->fields['name'],
                                "value"         => $result->fields['value'],
                                "value_array"   => $value_array,
                                "unit"          => $result->fields['unit'],
                                "subject"       => $result->fields['subject'],
                                "brief"         => $result->fields['brief'],
                                "if_multi"      => $result->fields['if_multi'],
                                "param"         => $param,
                                "rank"          => $result->fields['rank'],
                            );
                $result->MoveNext();
            }
        }
    }
}
if (empty($config_sort_info) ){
    $config_sort_sub    = func::db_select(DB_TABLE_CONFIG_SORT, "*", "", "", "cs_rank");
}

///////////////////////////////////////////////////////////
$array_input[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "input",
                     "from"     => "",
                     "check"    => "/^[a-z0-9-_\.]+@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,4}$/i",
                     "option"   => "",
                 );
$array_textarea[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "textarea",
                     "from"     => "",
                     "check"    => "/^.*$/s",
                     "option"   => "",
                 );
$array_member_group[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "select",
                     "from"     => "database",
                     "check"    => "/^[a-z0-9_]{1,32}$/i",
                     "option"   => array(
                                        "table"     => "member_group",
                                        "field"     => array("mg_type","mg_name"),
                                        "condition" => "",
                                        "groupby"   => "mg_type",
                                        "orderby"   => "mg_rank",
                                   ),
                 );
$array_language[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "select",
                     "from"     => "database",
                     "check"    => "/^[1-9][0-9]{0,10}$/",
                     "option"   => array(
                                        "table"     => "language",
                                        "field"     => array("id","name"),
                                        "condition" => "",
                                        "groupby"   => "",
                                        "orderby"   => "rank",
                                   ),
                 );
$array_template[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "select",
                     "from"     => "database",
                     "check"    => "/^[1-9][0-9]{0,10}$/",
                     "option"   => array(
                                        "table"     => "template",
                                        "field"     => array("id","name"),
                                        "condition" => "",
                                        "groupby"   => "",
                                        "orderby"   => "rank",
                                   ),
                 );
$array_radio[] =  array(
                     "name"     => "",
                     "subject"  => "",
                     "type"     => "radio",
                     "from"     => "",
                     "check"    => "/^[123]{1}$/",
                     "option"   => array(
                                        array(
                                            "candidate_value"   => "1",
                                            "candidate_name"    => "{\$lang.s.config.if_register.1}",
                                        ),
                                        array(
                                            "candidate_value"   => "2",
                                            "candidate_name"    => "{\$lang.s.config.if_register.2}",
                                        ),
                                   ),
                 );
$array_point_group =  array(
                            array(
                                "name"     => "topic_post",
                                "subject"  => "{\$lang.s.config.point_group.topic_post}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_reply",
                                "subject"  => "{\$lang.s.config.point_group.topic_reply}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_poll",
                                "subject"  => "{\$lang.s.config.point_group.topic_poll}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_elite",
                                "subject"  => "{\$lang.s.config.point_group.topic_elite}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_top",
                                "subject"  => "{\$lang.s.config.point_group.topic_top}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_close",
                                "subject"  => "{\$lang.s.config.point_group.topic_close}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_move",
                                "subject"  => "{\$lang.s.config.point_group.topic_move}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_upload",
                                "subject"  => "{\$lang.s.config.point_group.topic_upload}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "topic_download",
                                "subject"  => "{\$lang.s.config.point_group.topic_download}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "search",
                                "subject"  => "{\$lang.s.config.point_group.search}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "pm_send",
                                "subject"  => "{\$lang.s.config.point_group.pm_send}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                            array(
                                "name"     => "point_min",
                                "subject"  => "{\$lang.s.config.point_group.point_min}",
                                "type"     => "input",
                                "from"     => "",
                                "check"    => "/^[+-]?[0-9]{1,11}$/",
                                "option"   => "",
                            ),
                 );
//echo serialize($array_point_group);
///////////////////////////////////////////////////////////

$t->assign(array(
    "config_sort_sub"   => $config_sort_sub,
    "config_sort_info"  => $config_sort_info,
    "config_list"       => $config_list,
));
?>
